package com.sky.tests;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;


@SpringBootTest
public class testHttpClient {

    @Test
    public void httpClient() throws IOException {
        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.创建httpGet对象,指定请求路径
        HttpGet httpGet = new HttpGet("http://localhost:8080/admin/shop/status");
        //3.设置参数/请求头
        httpGet.setHeader("token","eyJhbGciOiJIUzI1NiJ9.eyJlbXBJZCI6MSwiZXhwIjoxNjg0MTk3Nzg2fQ.BSmwJaKHZrI2z98xgZNjAxTdrX5ZZyISyq9relnVN9M");
        //4.发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //5．获取响应数据
        int statusCode = response.getStatusLine().getStatusCode();//响应状态码
        // String body = EntityUltils.toString(response . getEntity());//获取响应体对象，并使用工具类解析成字符串
        String body = null;
        InputStream is = response.getEntity().getContent();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();//在内存中新建一个字节数组1/1．先通过输入流读取字节
        byte[] bytes = new byte[1024];
        int len;
        while ( (len = is.read(bytes)) != -1) {
//2.写到内存字节数组中
            bos.write(bytes,0,len);
        }
//3．将字节数组转换为字符串
        byte[] bys = bos.toByteArray();
        body = new String(bys);


    }

        /**
         * 测试通过httpclient发送POST方式的请求
         */
        @Test
        public void testPOST() throws Exception{
            // 创建httpclient对象
            CloseableHttpClient httpClient = HttpClients.createDefault();

            //创建请求对象
            HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("username","admin");
            jsonObject.put("password","123456");

            StringEntity entity = new StringEntity(jsonObject.toString());
            //指定请求编码方式
            entity.setContentEncoding("utf-8");
            //数据格式
            entity.setContentType("application/json");
            httpPost.setEntity(entity);

            //发送请求
            CloseableHttpResponse response = httpClient.execute(httpPost);

            //解析返回结果
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("响应码为：" + statusCode);

            HttpEntity entity1 = response.getEntity();
            String body = EntityUtils.toString(entity1);
            System.out.println("响应数据为：" + body);

            //关闭资源
            response.close();
            httpClient.close();
        }

    }




